* Settings
global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"

* Confirm that the Stata global for the project has been defined
assert !missing("$SSDIMed")

* All required Stata modules are available in the /_packages and /_auxiliary folders
cap adopath - "/home/site/etc/stata/ado.nber"
cap adopath - "/home/site/etc/stata/ado.nber/updates"
adopath ++ "$SSDIMed/scripts/_packages"
adopath ++ "$SSDIMed/scripts/_auxiliary"
capture log close 
log using $SSDIMed/scripts/logs/05_fig7b_fullmodel.log, text replace 
*cap do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do" 3in 5.9in

********************************************************************
*** full cost curves including flat region 

foreach DeltaC in -1000 -5000 {
local posDeltaC=-1*`DeltaC'
use "$SSDIMed/results/estimates/marginal_function/tablecolumn1.dta", clear 
rename (lb1 sd1 ub1 value1) (lb sd ub value)
if "`posDeltaC'"=="500" drop if regexm(var, "5000") | regexm(var, "50000")
if "`posDeltaC'"=="1000" drop if (regexm(var, "500") & !regexm(var, "5000")) | regexm(var, "50000")
if "`posDeltaC'"=="50000" drop if (regexm(var, "500") & !regexm(var, "50000"))  | (regexm(var, "5000") & !regexm(var, "50000")) 
foreach var in x51 x51UR x52 x52UR{
sum value if var=="`var'" 
local `var': di %3.0f = `r(mean)'
}
keep if inlist(var, "m", "mC_51`posDeltaC'", "mC_52`posDeltaC'", "mUR", "n", "nUR", "nC_51`posDeltaC'", "nC_52`posDeltaC'")
keep var value 
gen i=1 
reshape wide value, i(i) j(var) string 
rename value* * 
drop i 
expand 800
gen x=_n
sum x 
gen B=n+m*x 
gen BUR=nUR+mUR*x 
gen C51=nC_51`posDeltaC'+mC_51`posDeltaC'*x 
gen C52=nC_52`posDeltaC'+mC_52`posDeltaC'*x 
***find where the two cost curves intersect: xvalue is cross and y-value is flat 
local cross=(nC_51`posDeltaC' - nC_52`posDeltaC')/(mC_52`posDeltaC'*x - mC_51`posDeltaC'*x )
local flat = nC_51`posDeltaC'+mC_51`posDeltaC'*`cross' 
foreach var in C51  C52 {
replace `var'=`flat' if x<`cross'
replace `var'=`var'-`flat' 
replace `var'=`var'/10000
}
gen C51UR=(`DeltaC'/10000)+C51
gen C52UR=(`DeltaC'/10000)+C52
replace B=(B-`flat')/10000
replace BUR=(BUR-`flat')/10000
***cut off the cost curves
if "`posDeltaC'"=="1000" local ytop 1.2
if "`posDeltaC'"=="5000" local ytop 6

replace C51=. if C51>`ytop' 
replace C51UR=. if C51UR>`ytop'  
replace C52=. if C52>`ytop' 
replace C52UR=. if C52UR>`ytop' 
gen points=B if x==`x51' | x==`x52'
gen pointsUR=BUR if x==`x51UR' | x==`x52UR'
if "`posDeltaC'"=="1000" local arr "(pcarrowi 1.05 340 0.95 390, col(red)) (pcarrowi 0.95 645 0.85 695, col(green))" 
if "`posDeltaC'"=="5000" local arr "(pcarrowi 4.5 460 4.3 410, col(red)) (pcarrowi 4.9 640 4.7 690, col(green))" 

if "`posDeltaC'"=="1000"   local lab "text(`ytop'  -20 "$") text(1.2 5 "Marginal Benefits (Medical Spending)", placement(e))  text(1.07 340 "C{sup:51} (Estimated|{&Delta}C )", col(red) placement(s)) text(0.97 650 "C{sup:52} (Estimated|{&Delta}C )", col(green) placement(s))  text(-0.05 -10 "{&Delta}C=`posDeltaC' (Assumed)", placement(e))"
if "`posDeltaC'"=="5000"  local lab "text(`ytop'  -20 "$") text(4.2 5 "Marginal Benefits (Medical Spending)", placement(e))  text(4.8 500 "C{sup:51} (Estimated|{&Delta}C )", col(red) placement(s)) text(5.2 630 "C{sup:52} (Estimated|{&Delta}C )", col(green) placement(s))  text(-0.3 -10 "{&Delta}C=`posDeltaC' (Assumed)", placement(e))"

twoway (line B x, col(orange)) (line BUR x, lpat(dash) col(orange)) (line C51 x, col(red))   (line C51UR x, col(red) lpat(dash)) (line C52 x, col(green))   (line C52UR x, col(green) lpat(dash)) `arr' (scatter points x, msym(S) mcolor(black)) (scatter pointsUR x, msym(Oh) mcolor(black))  , legend(off) ytitle("") xtitle("Entrants Per Million") xlabel(0(200)800)  ylabel(none) graphregion(color(white))  bgcolor(white) `lab'
graph display, ysize(3) xsize(5.9)
graph export $SSDIMed/results/figures/estimatedmodel_full5152C`posDeltaC'.pdf, as(pdf) replace
if "`posDeltaC'"=="1000"   local lab "text(`ytop'  -20 "$") text(0.99 5 "Marginal Benefits (Medical Spending)", placement(e))  text(1.15 340 "C{sup:49} (Estimated|{&Delta}C )", col(red) placement(s)) text(1.05 650 "C{sup:50} (Estimated|{&Delta}C )", col(green) placement(s))  text(-0.05 -10 "{&Delta}C=`posDeltaC' (Assumed)", placement(e))"
if "`posDeltaC'"=="5000"  local lab "text(`ytop'  -20 "$") text(4.2 5 "Marginal Benefits (Medical Spending)", placement(e))  text(4.8 500 "C{sup:49} (Estimated|{&Delta}C )", col(red) placement(s)) text(5.2 630 "C{sup:50} (Estimated|{&Delta}C )", col(green) placement(s))  text(-0.3 -10 "{&Delta}C=`posDeltaC' (Assumed)", placement(e))"

twoway   (line B x,  col(orange)) (line BUR x, lpat(dash) col(orange)) (line C51 x, col(red))   (line C51UR x, col(red) lpat(dash)) (line C52 x, col(green))   (line C52UR x, col(green) lpat(dash))  `arr'  (scatter points x,  msym(S) mcolor(black)) (scatter pointsUR x, msym(Oh) mcolor(black)), legend(off) ytitle("") xtitle("Entrants Per Million") xlabel(0(200)800)  ylabel(none) graphregion(color(white))  bgcolor(white) `lab'
graph display, ysize(3) xsize(5.9)
graph export $SSDIMed/results/figures/estimatedmodel_full4950C`posDeltaC'.pdf, as(pdf) replace 
}


